package com.walter.learn.UseThread.ThreadDemo1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 * @ProjectName: learnOwner
 * @Package: com.walter.learn.UseThread
 * @ClassName: MyBufferCache
 * @Description: java类作用描述
 * @Author: 唐朝
 * @CreateDate: 2019/2/19 9:42
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/2/19 9:42
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MyBufferCache {
    private char[] tenCharacters = new char[10];
    private boolean isEnd = false;//判断是否读完文件
    private boolean isEndPre = false;//isEnd前一状态
    public boolean isSaved = false;//判断是否已经保存了一组字符

    public boolean isEnd() {
        return isEnd;
    }

    public synchronized void read() {
        try {
            // Read the file
            File myFile = new File("G:\\TestData\\threadRead.txt");
            FileReader fileReader = new FileReader(myFile);
            BufferedReader reader = new BufferedReader(fileReader);
            while (!isEnd) {
                if (isSaved) {
                    wait();
                } else {
                    for (int i = 0; i < 10; i++) {
                        int readOneCharacter;
                        do {
                            readOneCharacter = reader.read();//读一个字符
                        } while ('\r' == (char) (readOneCharacter) || '\n' == (char) (readOneCharacter));
                        char tmp = (char) (readOneCharacter);
                        if (readOneCharacter == -1) {//文档中无字符后结束保存
                            isEndPre = isEnd;
                            isEnd = true;
                            break;
                        }//end if
                        tenCharacters[i] = tmp;//保存在数组内
                    }//end for
                    isSaved = true;
                    notifyAll();
                }
                //end else
            }//end while
            reader.close();
        } catch (Exception ex) {
            System.out.println("Here");
            ex.printStackTrace();
        }
    }//end read method

    public synchronized void transform() {
        try {
            while (!isEndPre) {
                if (!isSaved) {
                    wait();
                } else {
                    System.out.println(tenCharacters);
                    //打印原始数组
                    for (int j = 0; j < 10; j++) {
                        if (Character.isUpperCase(tenCharacters[j])) {
                            System.out.print(Character.toLowerCase(tenCharacters[j]));
                        } else {
                            System.out.print(tenCharacters[j]);
                        }
                    }
                    System.out.println();
                    tenCharacters = null;//清空数组
                    tenCharacters = new char[10];
                    isSaved = false;
                    notifyAll();
                    isEndPre = isEnd;//放在这里而不是read()中，保证最后一组转换能够仅再执行一次
                }
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
